package com.bawei.module_home.ui.merchant

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseFragment
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_home.R
import com.bawei.module_home.adapter.MerchantAdapter
import com.bawei.module_home.databinding.FragmentMerchantBBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.MerchantModelItem
import dagger.hilt.android.AndroidEntryPoint
import kotlin.getValue
@AndroidEntryPoint
class MerchantBFragment : BaseFragment<HomeServiceViewModel, FragmentMerchantBBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_merchant_b
    val mViewModel: HomeServiceViewModel by viewModels()

    val adapter: MerchantAdapter by lazy {
        MerchantAdapter()
    }
    override fun initView() {
        super.initView()
        initAdapter()

        adapter.setOnItemClickListener(object : MerchantAdapter.OnItemClickListener{
            override fun clickListener(
                item: MerchantModelItem,
                position: Int
            ) {
                ARouter.getInstance().build(Router.MERCHANT_DETAIL_PAGE).withInt("merchantID",item.id).navigation()
            }

        })
    }

    override fun initData() {
        mViewModel.merchant()
    }

    override fun initListener() {
        mViewModel._mMerchantState.observeWithLifecycle(this){
            when(it){
                Empty ->{}
                is Failed ->{}
                Loading -> {}
                Success -> {
//                    ToastUtils.showLong(context,"成功")
                    adapter.setData(mViewModel.merchantBList)
                }
            }
        }
    }

    fun initAdapter(){
        mBinding.rv.layoutManager= LinearLayoutManager(context)
        mBinding.rv.adapter=adapter
    }


}